{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "import scipy as sp\n",
    "import sympy as sy\n",
    "sy.init_printing() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.set_printoptions(precision=3)\n",
    "np.set_printoptions(suppress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\" # display multiple results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "def round_expr(expr, num_digits):\n",
    "    return expr.xreplace({n : round(n, num_digits) for n in expr.atoms(sy.Number)})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font face=\"gotham\" color=\"purple\"> Matrix Operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Matrix operations are straightforward, the addition properties are as following:\n",
    "1. $\\pmb{A}+\\pmb B=\\pmb B+\\pmb A$\n",
    "2. $(\\pmb{A}+\\pmb{B})+\\pmb C=\\pmb{A}+(\\pmb{B}+\\pmb{C})$\n",
    "3. $c(\\pmb{A}+\\pmb{B})=c\\pmb{A}+c\\pmb{B}$\n",
    "4. $(c+d)\\pmb{A}=c\\pmb{A}+c\\pmb{D}$\n",
    "5. $c(d\\pmb{A})=(cd)\\pmb{A}$\n",
    "6. $\\pmb{A}+\\pmb{0}=\\pmb{A}$, where $\\pmb{0}$ is the zero matrix\n",
    "7. For any $\\pmb{A}$, there exists an $-\\pmb A$, such that $\\pmb A+(-\\pmb A)=\\pmb0$.\n",
    "\n",
    "They are as obvious as it shows, so no proofs are provided here.And the matrix multiplication properties are:\n",
    "1. $\\pmb A(\\pmb{BC})=(\\pmb{AB})\\pmb C$\n",
    "2. $c(\\pmb{AB})=(c\\pmb{A})\\pmb{B}=\\pmb{A}(c\\pmb{B})$\n",
    "3. $\\pmb{A}(\\pmb{B}+\\pmb C)=\\pmb{AB}+\\pmb{AC}$\n",
    "4. $(\\pmb{B}+\\pmb{C})\\pmb{A}=\\pmb{BA}+\\pmb{CA}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that we need to differentiate two kinds of multiplication, <font face=\"gotham\" color=\"red\">Hadamard multiplication</font> (element-wise multiplication) and <font face=\"gotham\" color=\"red\">matrix multiplication</font>: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 2], [3, 4]])\n",
    "B = np.array([[5, 6], [7, 8]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5, 12],\n",
       "       [21, 32]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A*B # this is Hadamard elementwise product"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19, 22],\n",
       "       [43, 50]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A@B # this is matrix product"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The matrix multipliation rule is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "43"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "22"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "50"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(A[0,:]*B[:,0]) # (1, 1)\n",
    "np.sum(A[1,:]*B[:,0]) # (2, 1)\n",
    "np.sum(A[0,:]*B[:,1]) # (1, 2)\n",
    "np.sum(A[1,:]*B[:,1]) # (2, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> SymPy Demonstration: Addition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's define all the letters as symbols in case we might use them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = sy.symbols('a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z', real = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHYAAAAzCAYAAABCMvzOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAG90lEQVR4Ae2bj3HVOBDG32NSQI7rIHTAHR2EDoASoANurgKG6wCughA6SDrgoAPogEAHue+nWEaWZXn9/O85o53Rs7ReSZ93Je3a0tvvdrtTpRbd3t7+bDEL4+g0sN/vk/Z7IKSflX5E6e3RPUEB1KWBy8h22PISw0JPNUP3QXp1xy6/x64B2axhO+F1tvOGPXb8Bd9ADZz0yWsNP5PMX5Xcn7reUNZI+VLxjuJiwVn5I5Yu9xx6hkdrgLdgnQLXVzVyrofcxUl8jHoZ8lXG/94qJeuEskvlhWUQTskTV7xbCl/Yz1CsYV1LXu2/VLpS2uUMy+g+jRsUzwVbMX+tsvAMwil5B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EaDkeZ1D+VPhPLR/S72xGFWfDQF2o5gsHI9J6lN8dzRG16xhaah1EE0884FxybJ32DpnZFGGVUbtj8Zp7etQubEYVd8ZU1f0yamUll3AJnKG7fOxNDL2wPgS53inwIlS5qSDMWoJpi5bo2x/kviHnV+Sk5j7DMtG+dgD44DyG+5JEBMwp8A5AYxsE2MwDp4cJzkoGhX+PFAs5jbfdb+xeR4EB4wmNod5GGQbJ+1UnpQOwMno/1vJb2Cz6U5AmJ0FY0APxUhf0ifLKkZlctyojMu5UVumILQzeFIDrSBHDWMoFvPG2VaV6Rw/UB+8Vh5fMKt/TWGEJ8rh5Jk52uOer5JtnN/19+a8dmGM+5RcS9+xjC9Ltj948sLhVRVRSnwQmxmAUVMHxj+H9ZfKd+AkykdJMc7X4iXfDObEm8IY9ycZj7keiLFMWObZlK6yS7EEGqSlgPconHe8FBCKY9xjPzDuD+TxT0GM6Q+J85eJueMAdfeLMrr8JXSXS7q9WCgumw0rIIwElq7YqLA5wonBax8leQzNaGM5Xox6cOIuPgrnKn8S80rowejF/PWJMvWRWc/su5oMKyD35cA4+hispD4lDrmf06UGXAobM7YRpFr663vdITKj4ftyYDylOKenSuEWnR0sM0CXYR98xv0UMiz57IwVEJZSQuz7cmCcWKDhSvSMuAz4pNnIqssQQFUHfIP9f/ZcsRq+VwfGUZqeyQdN/h2WuGF2n2vVZWRYXCDf6ht/MA9l4rz6IRYqB8ZjxRxTWUYiin8ow3Z9KGrB9Ybt9bGtmoUxqwZkGP7AhvuD8K8XLjfwpxh2oMIWEMfX856Nb8VNHPRntmzwtMBDlC7aGnCGFZureQmOmymGjTWyclkzdJK/dJaleGVDztW9n7Fnfk33HWnk/PT5cj1eDcR2E1Lnm/17LB8iQuLPtY0X+fBmyR+PBmRYvsWfR4iu/weyh58je1O3zgAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 a & 2 b & 2 c\\\\2 d & 2 e & 2 f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡2⋅a  2⋅b  2⋅c⎤\n",
       "⎢             ⎥\n",
       "⎣2⋅d  2⋅e  2⋅f⎦"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFUAAAAzCAYAAADmWEQdAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAC6klEQVR4Ae2bgVXcMAyGk74OwKMbXDegdAKuG8AKMAMj3AhlBboBbMDrbUA3aGGD49dd9F7OSLYTn3inPvk9k0SWo/iL7LMd0Xddd4L8Lm02m9d3whDsEej7XmT3CVq/kV+SvNqrHRcagfuEG3G8J6iUfsAz+1G+2Ynjb44AeO1xg+6WG0PN1Y2yiQQ+1+hj7ODh4C/0vyKv8Jb+1NRt1fFq+xkNXwJSJ2WU0Zh7yWU4p8GZ6ixYZnWEDVe28bzXyA/Z7g8vIaUTQPuF4zbhnGYFdP1zJ7H569l2FipwXSGvBWxPkC3RcHFKIejPEbm1XYK6BI1/AhHyVkpUbpXc2lahVnrhqQVR77ZVqIDFwNgrx/zYe626v2vbOahjiNr5F63gA+RHazsHlb1R4sOeRPNWi+Tatgp1mDoRMKmLs8xkAeDdtgp1cL9HHBfD+fjAnkrlVsmt7RJU2oU5F6h9g2w98ihBpVnk2nZpmUrl6TKVtrjOAFVc2h5KDhuubON5t8vUmg0V8spbzB15GPiO6wuAW+NonVza7kGFvOEGkCzHR2v4R3F/OB556lVpTD2Kh/X2EAHV4I0F1IBqQMDgluGpAdWAgMEtw1MDqgEBg1uGpwZUAwIGt6xZ+3dYfkUwxcQgktIulauAhgPukE1uN5w+gim0nj9sjMwOIin9ULkNaNCAVcqb2l2C6jagoRKeptbUbhUqugB/3NMMk5y/VeV0Jpd5t61CHQF7FajwJ+Qa8EL1oohflkvbOajFlkPhaAMaah6+QSfb7hxU9kbJNntSBFMIdFSomO9x15O6OMsimGIK1EHXbUCD0NYpoqZ2q546PIHrgIYpFBPd5naXlqmuAhoOuEyd3G68mAimSLwzvZwdyBHBFCnKhuthzyCCKRoYqlVLP1RqxSjQCQRUnc3skoA6G51eMaDqbGaX8DeqRbrdNlqmzr75/14xZYb2bpfvPKVaJADuADX+5z+Bkl4C6gNktKE9To9vhz1xlc3CFtQAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡0  0  0⎤\n",
       "⎢       ⎥\n",
       "⎣0  0  0⎦"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = sy.Matrix([[a, b, c], [d, e, f]])\n",
    "A + A\n",
    "A - A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAL8AAAAzCAYAAADGmNmlAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAKVElEQVR4Ae2di3HUOhSGEyYFcKGD0AGPCoAOgFsB0EEYKshAB0AFEDpIOuDRAXRwIR3k/p8iebRa27JsWfZupBnFtiydx6+joyN57RweHBzcVt5KV1dXl1uFtaAisIMIHB4ettr4LenyQ/lvkN/toI5V5IpAFwJnuhHa+BnGT3oqT3/o5dfXxfVvRWD3EZBdb9i3NDL27Yx/9zWsGlQEEhGoxp8AGLGj8rnyX+VfCU2zV12TLNmVm0hQ2Jwof4iRqcYfQ8i7zyYAU6iKfitfeLeKn65JluLKxxkS1kQ3bBYxfo3KJ8qv4jqstsZ9SXY+RbqMGEyWZYoeOdvmwkSO4Z7ym5hsixi/hGLrqXX7KSbw0vfpICvDVM8/GYOMsiwNq+M/GRNHaMhxKeMfItta65iwh7BjBQKuSZYVwJEmwtGQ6vIwx6rHNEKH/6f8VfmZDOC9joskyfRMjOl8t/BEpieS6ePMAuH5f4r/ieVzT8dz8YV/6YQsFxaLOzrHc4LJa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9O4XM1A01KCOS3PgIprIgc1G7TNm/Hwiw63cjcwiDmHnAU1Z1x91Qptxu48LJX+0qoMPBlDqBXYz6KVX0rQ+Iw7Ig4fH0O+pb5KMT21ypkciZsLioURnwgVMBn3ArNP4rZFj6P70jl4MCEDu9HTcL5iMQRbkl9TBc8nlOSEW+y7s4ak7a7Owz+YSw6eLx12Cry8D5028D0bCgpmxNfXG/LbFt6AlIYlRUsQX+WiVk0f8mYEGzUKuzcRjp+GDxUTaqc1DJ/TdEgAPZlfCspLpoZiFtlKSv+OFM3TrL7cb6O5tHDuN344YjNyASSsBCjGuHdClP1qFGH5C0ZKzEGsgw9MJIUyI/1kLlPZ6jYdzstijG6B3g/LZLqU/NkGUsJZo4Df9ElP4KFLhse6v5qNVLbJ2GUBL1elFcghs52HsDAK3x8/ga13bTOfYS4HOdQtwZLiUXGy18iEpvmV02ts6783kxW5e9hvU6AuiE8LB3q3nXuMHUEtog3pHWVinxDVeuGQnY2RJi925QJAcDPyNtKBsyBLdXdkQdqYLYdDsBMZYdIY9sYYT7zOoyElJHo2PVpnYWkdCMLzfYGWTmJWpPAqHMqL1c6Ef6A9bi3j/c3+LpLtFcOn1/EniJlTW6EyODQU0UyuG70BmSuMpa/IgShB11qpjcJhVoDTihH5NbC1dsnn+UrgsYvxpGF/XFiD8Ezi8/LGOxLp8Fjx5EI3hXdu0IoDxM/Ny7H0o2dp6BYU7Y/xgJWNfYmG5gm5anwjqi10ONw2gS8X86+vNKtGNQ8B5fkIJprAmaWTvbCzdKFFPKgJCILRtFRlbP9QJe8I8pPDTxxpi+HDU811GQMbPE98ngQ4X/wPmsT0aAmVxbgAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a + g & b + h & c + i\\\\d + j & e + k & f + l\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡a + g  b + h  c + i⎤\n",
       "⎢                   ⎥\n",
       "⎣d + j  e + k  f + l⎦"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAL8AAAAzCAYAAADGmNmlAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAJV0lEQVR4Ae2djXXUOBDHk7wUwHEdhA74qIBcB4QODjoI7yrgQQdABRA6SKjgSDqADoB0kPv/tBqfVusPedeW7UXznmJbtjUzf41GI2mtHB4cHNxT2qC7u7vbjcySURBYIAKHh4e1Nn4kXa6VfkXpzQJ1LCIXBJoQuNCN2MYvMH7oL3n6wyC9XGWXvwWB5SMgu16zb2nk7NuMf/kaFg0KAj0RKMbfAzBiR6VLpV9K33q8Ovijc5JlcOV2LFDYnCu96yqmGH8XQsF9JgHoQpX1XekquJX9dE6yZFe+myFhTeeEzXF3OeWJGgQeKu91Tf4UWXOSZQr9N3jKMTzYyKzJKJ6/BpS2LHWnp/7+pJ4fGeYkSxtmc71XjL9/zbiwh7Cj/6uDvzEnWQZXbuwCk8IeeZgTCfJKiQr/ofRZ6ZkM4K2Ok5BkeibGVL4NPJHpVDK9H1kgPP+N+J97PnSxl+IL/9yELFcei/s6ZzEHTF5KHsYl2UgysDaEbUB/Kn2UDDfuKsMf8Uf3F0rUxzfxTrJNjAejOahL3FNigeDE7uv8UulO6Z7l5TyKL0BfG0+d0ziRESOs1WOIfJUPwOjNwqDTPcir8BmCV1cZAV9kqXjrnAWdi673h7oPbyVsCGfosNc5My2j1oXxsqP4veFc5OzV8uuOeoZGctka9vjWBJivVEjoSTj/rjx6gqzkvRxe96kx9rJhmDTKMQlgoaemux2VR0+Uk0yWv73+IW+wyEVg/lkyhD3fc+WNXReVfrIJGuC/PuNMx9BWq+fik+M4I7rGwwLkpygf4ENlo9urS994vuiqT2WcCci27vKDyrsKjI6BHzMeUKdMq8e2/ktIcRPxNt3o6jdoJAzgY7LEWFE3WWaipBseHsNb4yd8/lBeI42AyU/xtLrHqxOid1KX8dOCY0OjslG4s2V7I3nUKUXiA97I4R8vYFDhzMEnt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bH4cHMxYU+xuILA0AiY5yeUcLGbMSixtCFRjktHILZt6eNs3aY6WaQIiX8uUEKMEJFyvlgEZPys+J5GClz9BxaXwqcHM1M1AAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a - g & b - h & c - i\\\\d - j & e - k & f - l\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡a - g  b - h  c - i⎤\n",
       "⎢                   ⎥\n",
       "⎣d - j  e - k  f - l⎦"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = sy.Matrix([[g, h, i], [j, k, l]])\n",
    "A + B\n",
    "A - B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> SymPy Demonstration: Multiplication"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The matrix multiplication rules can be clearly understood by using symbols."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFYAAAAzCAYAAAANb/8eAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAE7klEQVR4Ae2ajVEbMRCF7QwFkKQDpwMSKgA6gJQQOoBJBRnSQaACfjowHSTQQeggQAfO+w7JI3TSnXXWnQ1oZ9Yn6XS72qfVSpY0Ho1Gm+IazWazx1phKaghMB6Pg/i9U80b8YPHJzUJpSCGwKWHHVheAiy0Jw8dO3z4VFx+2xAQZs+wU/0KOwts2/flfSICaw0s8Us8FT+I/ybattLqaw0sEyhDTQjdia9XilSi8rUG1rFlS+mpk1/75NoDqxCwa1AsHpvZnapQQFjILLdXcRuLSJfXTFTvWIxx/8RX4n0Z+1PPvgmPvVUbjoyiT3pOpZs2DEbSz9oe26GP4nO14bbKRX6YbXdVaRRi3olZ9E7se6WJdzPxpi3r44l8o4c/MZUup2zenj50W5nSh1OBEY5UYaT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7zWXkJz2xoqc99nS6sNq564cCSuOiVRNBQkSXlllTVCuOzH+h0iviaPzAJshV3th9BDCCK2EnqSPbYxFNTUvZ2CCliZy7NT2CvABpxFHrr0NdESCgLA5iiyHjux8cQKNSsCmy3PCAI+bqpWxWV7P5ZFsEtcrB1yrejqflFpAcsu267X6Ov/LrwOOtqjtmEAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c\\\\d & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡a  b  c⎤\n",
       "⎢       ⎥\n",
       "⎣d  e  f⎦"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGIAAABLCAYAAACLBlLwAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAII0lEQVR4Ae2d8VEcNxTGuQx/Zxw8kwKgA5t0YDqI7QpiOsDjCjLQgZ0KYtMBdGBDB1BAZmKYNHD5fkJv0Ym929XeSnu56M0I7WolvafvPT1pJd0y29nZeaFwpdBG5/P5/HXbg5qWhsBsNrtRif22UsJ4ths8ONM1mUO6DW/q9VoInLaUPlLar6SHivgozVTgW9AaI0nYforrUS8hySnih/jhf/FeDXqmcKFwpxD36uJNkgwnCh9TGG+FImRt9wp0c3r0ZQoAmfIeq977lLpD15RSblPzMvH4fWrhZBQHqTJsRY+g0XIFr3zjN6FHpOphZ2sUoZY714SbSkZhAwqs7ZpkiYz6gGCD5LmuXwmQJ7OEzO2lR1x6efZ0/UwBuY4lS/bZoPjC750CbulGPHkdSCL86lxhX4V3UoLKMDe+sjLUoXCncGFpJWLxAwTawItp0w5dfyEUkuEUPiIM4q4PT+VDcco6X3iPUFp/8pZ3ohI/WSlVeOst48LSCsU2PvyGDBFPlJSV1GYM8KtnwkpELEMn/3XGiD9U+6Uafm9cJBC9C8I9lSRc0LVkuY6YoqASRvFdvK3NWPmfkRydt4MU4QHH0uKXFhrO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9/Cv2k9L8a+PAxwFCNpQAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}g & h & i\\\\j & k & l\\\\m & n & o\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡g  h  i⎤\n",
       "⎢       ⎥\n",
       "⎢j  k  l⎥\n",
       "⎢       ⎥\n",
       "⎣m  n  o⎦"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = sy.Matrix([[a, b, c], [d, e, f]])\n",
    "B = sy.Matrix([[g, h, i], [j, k, l], [m, n, o]])\n",
    "A\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZMAAAAzCAYAAABSd+U7AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAUj0lEQVR4Ae2djbXUthaF4S4K4JEOSAc8qCBJB0AqCOmALCpgkQ4eqQBIB9ABkA6gg8DtgLc/jY6QNf6RLcszvldnLY1t6UjaOto6kmXPzM0bN27cVjiSb9++XR5FtohmgWaBZoFmgWttgZs3b/bOGReyykeFr0l4ca2t1RrfLNAs0CzQLDBkgTdKSOeMN0wmyC+6E7kZhd8P0e2zWaBZoFmgWaBZ4LsFNE905guluPnCJpPvmu2sWaBZoFmgWaBZYKYFrvxkwv6ewluFrwqfcu0j3Xs+X+/+YG45TW+eBWTzRf01r5Z87Vw8uXr5NTfNORaQ/Z8q/G9OnjV0c/tdei8UPil8U7i3Rt3nVsaVn0x4kYDbMhn+s8K7GR3ws3TvkH9GnqZaaIGC/iqsuT97Lp5cvf5aWuwKFmCrZfOxmtvv0vtD+NyzaJ3/s0J7z66IXUwmmsl/VnhSaD1WA29zy1CH/6nw31z9WG8lvHGR1/F8Vn+NGWil/sjFk6s3BtmlrYR7sp6roKCx+qMCDnu2rGTnnH7HnyyaSFbCONs2czLsYjJRg9hqWrzdREd4o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==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a g + b j + c m & a h + b k + c n & a i + b l + c o\\\\d g + e j + f m & d h + e k + f n & d i + e l + f o\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡a⋅g + b⋅j + c⋅m  a⋅h + b⋅k + c⋅n  a⋅i + b⋅l + c⋅o⎤\n",
       "⎢                                                 ⎥\n",
       "⎣d⋅g + e⋅j + f⋅m  d⋅h + e⋅k + f⋅n  d⋅i + e⋅l + f⋅o⎦"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AB = A*B; AB"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> Commutability"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The matrix multiplication usually do not commute, such that $\\pmb{AB} \\neq \\pmb{BA}$. For instance, consider $\\pmb A$ and $\\pmb B$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEoAAAAzCAYAAAAw/Z54AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAFT0lEQVRoBe2bi1VcNxCGd3NSAHYqMO7Ajw5IB6YExx2QkxJwB1BCcAcmFTikA9NBwB2Q77srae/VfWmzT+DOOVq9RprRr9FIV4L5bDY7IrTo4eHhR6vwGRTM5/NOPBz6d8JDFi4AavYcAzh8zbAQm68/8SP9CijzWvi0KH5+v2DQwAIEKiwiUM8PkRVH/PMYP2v2GJ7fA9874jvzIP9PKKuiwBct0XVuu3P4rut820yjwxv6vyK8RW7Lx66roz7qhI5bPolyB3tVryN/TnDdpjakBabh18j/RpDvQ739ptP0r2zBuSDcEJR5lMuxjLCyjrRxHPqtypmnQdcFUKcCXULvKb+PvKTPCA1QbBfKbiLftuOaHl06/y8d6bMCasxHncB4j8k66Dq5nI6CKVv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+rUda9QpgNepoE1fGIFBjFuUptwuMyvEh+CAcuiAwYLf/1iMt5WNW7xi1wLgE7a5FY0C5zt3lcnKdH8yrDWDokHsfaVVeIAmN81YA0bZ1py97iwaXHtwXdOZRIJkreU/eUt+2vKjd0W8YrM577JFWy8odtsvOp7rRlTEIFB3cosi5YIVxu+QU9oq6hqnCo7IuU01Zaj1CLoo3/qtcQeg6XScA0PczOnpm+oNYJdS1+K+WpwdQIRsgQHUCpgfQAYwaVWPOvMH8nDMTUIWzPwE1AVWIQCFbPB743eZ2mSjf/lPFE0/kODDchEu8D/f+NIbeq1EA3Pi99yH1CQad/9j4H2aBZIH3HIYIAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}23 & 13\\\\51 & 29\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡23  13⎤\n",
       "⎢      ⎥\n",
       "⎣51  29⎦"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEoAAAAzCAYAAAAw/Z54AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAE6klEQVRoBe2ai1EbMRCG7UwKCCmBdMCjAkwHECoAOkiGEqADSAUEOjAdJNBB0kEGd+D830VS9uR7LOZ8JPg0IyStVtrVr9Vap2U8Go3eKS+k+Xw+WyCuAWE8HlfiwdJ/KM+zfCmgRuuYhcM0wwJspm/0h7QvUMYmn/4hr99fYVDCQggUWESg1g+RJ674bRu/zuxmRFUl55f2uZC/U7mQxH+eEa/F+5DRVtqUDlsScCq5jSfDyxeVxUdNNOmCTxIdYEr+Su0TZc7tgR2jNgCW5lL7Bprl66Me9Lhpk+XhEw/rTT5K9coE04mQPzC9X0P9zNCoAsqdlLOWBtA/6ewrSddPHllevjhX29HjyMxCLsYIiJmExPFFGYDE3Pdsh3j3bXvVdemBDiV9q2R6+ezYRmeOdShvUMZBARSal5GmEuuaiQ8lXzIdSYcr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LnNVoH//LDTfybhBeBRZXuQOkqQPLoiFzpuXSQdgiAtuycNmEIgLZgVOruykeVJn2NjQEo564OQA1AORFwssXrAf/GZ7+w+Sltuu47p///2HIctIKEC2/IPNnaXHouFWiVz6SvkS4ceBq2WFCf/gbwa9tlU96O/QAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}36 & 44\\\\13 & 16\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡36  44⎤\n",
       "⎢      ⎥\n",
       "⎣13  16⎦"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = sy.Matrix([[3, 4], [7, 8]])\n",
    "B = sy.Matrix([[5, 3], [2, 1]])\n",
    "A*B\n",
    "B*A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "How do we find commutable matrices?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKgAAAAzCAYAAAAD6kM0AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAJ30lEQVR4Ae2cjXXUOBDHE14K4LgOQgccVADXAXAVAB3AowIe1wFQAYQOkg4upAPSwYV0kPv/tBqdrPWH7JU/kvW8p7UsjUczf42ksdfW4cHBwX2lLbq5ubneKlwLVgRGQuDw8LDWD++pvR9Kv5L0cSQ9VrErAk0InKgi9cMTHBT6UzPmYZTebIrX3xWBaRCQ71V8UK06HzQHnUaLtZUVgZ4I3EkHJZ5ROlX6pfSzJya3hl22fVL66FNtDDeWMWozC+NcviY976SDcoPHkiGjL5XOmoy/zeU4pfR/oPRV6bXSM6XJKBfjXL4mxRfroOqAZ0oAvws90sWnuwhY8LVgg3PipOdKvQfixBgP6osjGbZUYskavGwBvjesd8ctFRDTy9sGNmfMUBytrudxEox36YvFzqA9ga5jd0u878C6+ttcthTbcvXI5dvqk6wZVCPgWFe+U2K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Mg9Spab1XL5Y746fVVmS0zFRpXT4egeQhdkiRhkv2K5pfPIVwohUo588eMYP3N4u3gkZxKM0QOdlSp7c+mcSY/KEBKmOquOuLt5byaNLjqWlHq+irYofB+9VTNRQYu+hCMMknRpfOxVS+1jxKe8nnX3Q4ueXcIZUKmuXdcUrW/RvRZjz48zpuESM/duGzeoQ6+9dYyALVLjtnQ0Oqd4GKlTE58Qx9S00YTjkZ1Bfw8odqWAxvJ2yvfANW2HAZXalvJMdZ7q0YRxreNKSWZHF0IJ82EbN3hL+dyAvwgrJKEsN1e+kLw5qyuio5WITScb8b7jaY8R6wjjleH8fFNyEG80McvGDV6PHFw9q4sVsYHVbLSZPTTWkhmAcdNqhC1ffVPDN26QQm9wRpKE2fNN5H6wmUDHJW3YkL0hAvrLLgDEPmaEyTZuyMEVkCPqfYMUXVs6m42xGsYRT2oUuL0bN8hZCAsm3bDBAFTbhDOL27hBerEa7cWGDVFfsPotcuMGYl/SaKQOX/zGDQxUJZt9iD9tSSyBy+gYl1ASGUelBJWSo+VvijjrvfQNcbMcgaWIZ6JL2riBsGpvN2yQ7Y4W56Cm2MhHlkxu8pa8cQMOysDhuHcbNshmR3vpoH6WnmKmNpx7H6XjXm/YYIDds8x6XBFYIgI2gx77OCzoqBE86o1KaGjNrAgIgdT/VER4c2D7g4aH2xSKPstB614k2NSuvysChRGQg/LvXbhx9eLP/gOgjE4W+BhF0wAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a e + b g & a f + b h\\\\c e + d g & c f + d h\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡a⋅e + b⋅g  a⋅f + b⋅h⎤\n",
       "⎢                    ⎥\n",
       "⎣c⋅e + d⋅g  c⋅f + d⋅h⎦"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKgAAAAzCAYAAAAD6kM0AAAACXBIWXMAA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
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a e + c f & b e + d f\\\\a g + c h & b g + d h\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡a⋅e + c⋅f  b⋅e + d⋅f⎤\n",
       "⎢                    ⎥\n",
       "⎣a⋅g + c⋅h  b⋅g + d⋅h⎦"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = sy.Matrix([[a, b], [c, d]])\n",
    "B = sy.Matrix([[e, f], [g, h]])\n",
    "A*B\n",
    "B*A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To make $\\pmb{AB} = \\pmb{BA}$, we can show $\\pmb{AB} - \\pmb{BA} = 0$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "M = A*B - B*A\n",
    "M"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\begin{align}\n",
    "b g - c f&=0 \\\\\n",
    " a f - b e + b h - d f&=0\\\\\n",
    "- a g + c e - c h + d g&=0 \\\\\n",
    "- b g + c f&=0\n",
    "\\end{align}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we treat $a, b, c, d$ as coefficients of the system, we and extract an augmented matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A_aug = sy.Matrix([[0, -c, b, 0], [-b, a-d, 0, b], [c, 0, d -a, -c], [0, c, -b, 0]]); A_aug"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Perform Gaussian-Jordon elimination till row reduced formed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A_aug.rref()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The general solution is \n",
    "\\begin{align}\n",
    "e - \\frac{a-d}{c}g - h &=0\\\\\n",
    "f - \\frac{b}{c} & =0\\\\\n",
    "g &= free\\\\\n",
    "h & =free\n",
    "\\end{align}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "if we set coefficients $a = 10, b = 12, c = 20, d = 8$, or $\\pmb A = \\left[\\begin{matrix}10 & 12\\\\20 & 8\\end{matrix}\\right]$ then general solution becomes\n",
    "\n",
    "\n",
    "\\begin{align}\n",
    "e - .1g - h &=0\\\\\n",
    "f - .6 & =0\\\\\n",
    "g &= free\\\\\n",
    "h & =free\n",
    "\\end{align}\n",
    "Then try a special solution when $g = h = 1$\n",
    "\\begin{align}\n",
    "e  &=1.1\\\\\n",
    "f & =.6\\\\\n",
    "g &=1 \\\\\n",
    "h & =1\n",
    "\\end{align}\n",
    "And this is a <font face=\"gotham\" color=\"red\">commutable matrix of $A$</font>, we denote $\\pmb C$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C = sy.Matrix([[1.1, .6], [1, 1]]);C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can see that $\\pmb{AB}=\\pmb{BA}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = sy.Matrix([[10, 12], [20, 8]])\n",
    "A*C\n",
    "C*A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font face=\"gotham\" color=\"purple\"> Transpose of Matrices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Matrix $A_{n\\times m}$ and its transpose is \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 2, 3], [4, 5, 6]]); A\n",
    "A.T # transpose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = sy.Matrix([[1, 2, 3], [4, 5, 6]]); A\n",
    "A.transpose()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The properties of transpose are"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. $(A^T)^T$\n",
    "2. $(A+B)^T=A^T+B^T$\n",
    "3. $(cA)^T=cA^T$\n",
    "4. $(AB)^T=B^TA^T$\n",
    "\n",
    "We can show why this holds with SymPy:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = sy.Matrix([[a, b], [c, d], [e, f]])\n",
    "B = sy.Matrix([[g, h, i], [j, k, l]])\n",
    "AB = A*B\n",
    "AB_tr = AB.transpose(); AB_tr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A_tr_B_tr = B.transpose()*A.transpose()\n",
    "A_tr_B_tr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "AB_tr - A_tr_B_tr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font face=\"gotham\" color=\"purple\"> Identity and Inverse Matrices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> Identity Matrices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Identity matrix properties:\n",
    "$$\n",
    "AI=IA = A\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's generate $\\pmb I$ and $\\pmb A$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "I = np.eye(5); I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.around(np.random.rand(5, 5)*100); A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A@I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "I@A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> Elementary Matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An elementary matrix is a matrix that can be obtained from a single elementary row operation on an identity matrix. Such as:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\left[\\begin{matrix}1 & 0 & 0\\cr 0 & 1 & 0\\cr 0 & 0 & 1\\end{matrix}\\right]\\ \\matrix{R_1\\leftrightarrow R_2\\cr ~\\cr ~}\\qquad\\Longrightarrow\\qquad \\left[\\begin{matrix}0 & 1 & 0\\cr 1 & 0 & 0\\cr 0 & 0 & 1\\end{matrix}\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The elementary matrix above is created by switching row 1 and row 2, and we denote it as $\\pmb{E}$, let's left multiply $\\pmb  E$ onto a matrix $\\pmb  A$. Generate $\\pmb A$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = sy.randMatrix(3, percent = 80); A # generate a random matrix with 80% of entries being nonzero"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "E = sy.Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]]);E"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It turns out that by multiplying $\\pmb E$ onto $\\pmb A$, $\\pmb A$ also switches the row 1 and 2. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "E*A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Adding a multiple of a row onto another row in the identity matrix also gives us an elementary matrix.\n",
    "\n",
    "$$\n",
    "\\left[\\begin{matrix}1 & 0 & 0\\cr 0 & 1 & 0\\cr 0 & 0 & 1\\end{matrix}\\right]\\ \\matrix{~\\cr ~\\cr R_3-7R_1}\\qquad\\longrightarrow\\left[\\begin{matrix}1 & 0 & 0\\cr 0 & 1 & 0\\cr -7 & 0 & 1\\end{matrix}\\right]\n",
    "$$\n",
    "\n",
    "Let's verify with SymPy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "A = sy.randMatrix(3, percent = 80); A\n",
    "E = sy.Matrix([[1, 0, 0], [0, 1, 0], [-7, 0, 1]]); E"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "E*A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also show this by explicit row operation on $\\pmb A$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "EA = sy.matrices.MatrixBase.copy(A)\n",
    "EA[2,:]=-7*EA[0,:]+EA[2,:]\n",
    "EA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will see an importnat conclusion of elementary matrices multiplication is that an invertible matrix is a product of a series of elementary matrices."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> Inverse Matrices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If $\\pmb{AB}=\\pmb{BA}=\\mathbf{I}$, $\\pmb B$ is called the inverse of matrix $\\pmb  A$, denoted as $\\pmb B= \\pmb A^{-1}$.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NumPy has convenient function ```np.linalg.inv()``` for computing inverse matrices. Generate $\\pmb A$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.round(10*np.random.randn(5,5)); A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "Ainv = np.linalg.inv(A)\n",
    "Ainv\n",
    "A@Ainv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ```-0.``` means there are more digits after point, but omitted here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <font face=\"gotham\" color=\"purple\"> $[A\\,|\\,I]\\sim [I\\,|\\,A^{-1}]$ Algorithm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A convenient way of calculating inverse is that we can construct an augmented matrix $[\\pmb A\\,|\\,\\mathbf{I}]$, then multiply a series of $\\pmb E$'s which are elementary row operations till the augmented matrix is row reduced form, i.e. $\\pmb A \\rightarrow \\mathbf{I}$. Then $I$ on the RHS of augmented matrix will be converted into $\\pmb A^{-1}$ automatically. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can show with SymPy's ```.rref()``` function on the augmented matrix $[A\\,|\\,I]$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "AI = np.hstack((A, I)) # stack the matrix A and I horizontally\n",
    "AI = sy.Matrix(AI); AI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "AI_rref = AI.rref(); AI_rref"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Extract the RHS block, this is the $A^{-1}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Ainv = AI_rref[0][:,5:];Ainv # extract the RHS block"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I wrote a function to round the float numbers to the $4$th digits, but this is not absolutely neccessary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "round_expr(Ainv, 4) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can verify if $AA^{-1}=\\mathbf{I}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = sy.Matrix(A)\n",
    "M = A*Ainv\n",
    "round_expr(M, 4) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We got $\\mathbf{I}$, which means the RHS block is indeed $A^{-1}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <font face=\"gotham\" color=\"purple\"> An Example of Existence of Inverse"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Determine the values of $\\lambda$ such that the matrix\n",
    "$$A=\\left[ \\begin{matrix}3 &\\lambda &1\\cr 2 & -1 & 6\\cr 1 & 9 & 4\\end{matrix}\\right]$$\n",
    "is not invertible."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Still,we are using SymPy to solve the problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lamb = sy.symbols('lamda') # SymPy will automatically render into LaTeX greek letters\n",
    "A = np.array([[3, lamb, 1], [2, -1, 6], [1, 9, 4]])\n",
    "I = np.eye(3)\n",
    "AI = np.hstack((A, I))\n",
    "AI = sy.Matrix(AI)\n",
    "AI_rref = AI.rref()\n",
    "AI_rref"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To make the matrix $A$ invertible we notice that are one conditions to be satisfied (in every denominators):\n",
    "\\begin{align}\n",
    "-6\\lambda -465 &\\neq0\\\\\n",
    "\\end{align}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solve for $\\lambda$'s."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sy.solvers.solve(-6*lamb-465, lamb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's test with determinant. If $|\\pmb A|=0$, then the matrix is not invertible. Don't worry, we will come back to this. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[3, -155/2, 1], [2, -1, 6], [1, 9, 4]])\n",
    "np.linalg.det(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The $|\\pmb A|$ is practically $0$. The condition is that as long as $\\lambda \\neq -\\frac{155}{2}$, the matrix $A$ is invertible."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <font face=\"gotham\" color=\"purple\"> Properties of Inverse Matrices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. If $A$ and $B$ are both invertible, then $(AB)^{-1}=B^{-1}A^{-1}$.\n",
    "2. If $A$ is invertible, then $(A^T)^{-1}=(A^{-1})^T$.\n",
    "3. If $A$ and $B$ are both invertible and symmetric such that $AB=BA$, then $A^{-1}B$ is symmetric."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The <font face=\"gotham\" color=\"red\"> first property</font> is straightforward\n",
    "\\begin{align}\n",
    "ABB^{-1}A^{-1}=AIA^{-1}=I=AB(AB)^{-1}\n",
    "\\end{align}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The <font face=\"gotham\" color=\"red\"> second property</font> is to show\n",
    "$$\n",
    "A^T(A^{-1})^T = I\n",
    "$$\n",
    "We can use the property of transpose\n",
    "$$\n",
    "A^T(A^{-1})^T=(A^{-1}A)^T = I^T = I\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The <font face=\"gotham\" color=\"red\">third property</font> is to show\n",
    "$$\n",
    "A^{-1}B = (A^{-1}B)^T\n",
    "$$\n",
    "Again use the property of tranpose\n",
    "$$\n",
    "(A^{-1}B)^{T}=B^T(A^{-1})^T=B(A^T)^{-1}=BA^{-1}\n",
    "$$\n",
    "We use the $AB = BA$ condition to continue\n",
    "\\begin{align}\n",
    "AB&=BA\\\\\n",
    "A^{-1}ABA^{-1}&=A^{-1}BAA^{-1}\\\\\n",
    "BA^{-1}&=A^{-1}B\n",
    "\\end{align}\n",
    "The plug in the previous equation, we have\n",
    "$$\n",
    "(A^{-1}B)^{T}=BA^{-1}=A^{-1}B\n",
    "$$"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
